package com.ocean.web.controller.product;

import java.util.List;

import com.ocean.common.core.domain.entity.SysDept;
import com.ocean.product.service.IOProductDeptService;
import com.ocean.productDict.domain.OProductRepairTime;
import com.ocean.productDict.domain.*;
import com.ocean.productDict.service.*;
import com.ocean.system.domain.OZnwgSq;
import com.ocean.system.service.ISysDeptService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ocean.common.annotation.Log;
import com.ocean.common.enums.BusinessType;
import com.ocean.product.domain.OProduct;
import com.ocean.product.service.IOProductService;
import com.ocean.common.core.controller.BaseController;
import com.ocean.common.core.domain.AjaxResult;
import com.ocean.common.utils.poi.ExcelUtil;
import com.ocean.common.core.page.TableDataInfo;

/**
 * 产品类别Controller
 *
 * @author ocean
 * @date 2023-01-05
 */
@Controller
@RequestMapping("/product/product")
public class OProductController extends BaseController
{
    private String prefix = "product/product";

    @Autowired
    private IOProductService oProductService;
    @Autowired
    private IOProductTypeService productTypeService;
    @Autowired
    private IOForecastTypeService forecastTypeService;
    @Autowired
    private IOAgingService agingService;
    @Autowired
    private IOForecastElementService forecastElementService;
    @Autowired
    private IOFrequencyService frequencyService;
    @Autowired
    private IOForecastIntervalService forecastIntervalService;
    @Autowired
    private IOHistoryDaysService historyDaysService;
    @Autowired
    private IOProductLateTimeService productLateTimeService;
    @Autowired
    private IOProductRepairTimeService productRepairTimeService;
    @Autowired
    private ISysDeptService sysDeptService;
    @Autowired
    private IOProductDeptService productDeptService;

    @RequiresPermissions("product:product:view")
    @GetMapping()
    public String product(ModelMap mmap)
    {
        List<SysDept> deptList = sysDeptService.selectDeptList(new SysDept());
        mmap.put("deptList",deptList);
        List<OProductType> oProductTypes = productTypeService.selectOProductTypeList(new OProductType());
        mmap.put("productTypes",oProductTypes);
        return prefix + "/product";
    }

    /**
     * 查询产品类别列表
     */
    @RequiresPermissions("product:product:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(OProduct oProduct)
    {
        startPage();
        List<OProduct> list = oProductService.selectFormList(oProduct);
        return getDataTable(list);
    }
    @PostMapping("/listsms")
    @ResponseBody
    public TableDataInfo listsms(OProduct oProduct)
    {
        startPage();
        List<OProduct> list = oProductService.selectFormListsms(oProduct);
        return getDataTable(list);
    }

    @PostMapping("/listznwgsms")
    @ResponseBody
    public TableDataInfo listznwgsms(OZnwgSq oZnwgSq)
    {
        startPage();
        List<OZnwgSq> list = oProductService.selectFormListsmsznwg(oZnwgSq);
        return getDataTable(list);
    }
    /**
     * 获取产品列表
     * @param oProduct
     * @return
     */
    @PostMapping("/data")
    @ResponseBody
    public AjaxResult data(OProduct oProduct)
    {
        List<OProduct> list = oProductService.selectFormList(oProduct);
        return AjaxResult.success(list);
    }
    /**
     * 导出产品类别列表
     */
    @RequiresPermissions("product:product:export")
    @Log(title = "产品类别", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(OProduct oProduct)
    {
        List<OProduct> list = oProductService.selectOProductList(oProduct);
        ExcelUtil<OProduct> util = new ExcelUtil<OProduct>(OProduct.class);
        return util.exportExcel(list, "产品类别数据");
    }

    /**
     * 检查产品命名是否重复
     * @param pname
     * @return
     */
    @PostMapping("/isjc")
    @ResponseBody
    public AjaxResult isjc(String pname){
        List<OProduct> list = oProductService.isjc(pname);
        return AjaxResult.success(list);
    }

    /**
     * 新增产品类别
     */
    @GetMapping("/add")
    public String add(OProduct product,ModelMap mmap)
    {
        List<SysDept> deptList = sysDeptService.selectDeptList(new SysDept());
        mmap.addAttribute("deptList",deptList);
        List<OProductRepairTime> oProductRepairTimes = productRepairTimeService.selectOProductRepairTimeList(new OProductRepairTime());
        mmap.addAttribute("repairTimes",oProductRepairTimes);
        List<OProductLateTime> oProductLateTimes = productLateTimeService.selectOProductLateTimeList(new OProductLateTime());
        mmap.addAttribute("productLateTimes",oProductLateTimes);
        List<OHistoryDays> oHistoryDays = historyDaysService.selectOHistoryDaysList(new OHistoryDays());
        mmap.addAttribute("historyDays",oHistoryDays);
        List<OForecastInterval> oForecastIntervals = forecastIntervalService.selectOForecastIntervalList(new OForecastInterval());
        mmap.addAttribute("forecastIntervals",oForecastIntervals);
        List<OFrequency> oFrequencies = frequencyService.selectOFrequencyList(new OFrequency());
        mmap.addAttribute("frequencies",oFrequencies);
        List<OForecastElement> oForecastElements = forecastElementService.selectOForecastElementList(new OForecastElement());
        mmap.addAttribute("elements",oForecastElements);
        List<OAging> oAgings = agingService.selectOAgingList(new OAging());
        mmap.addAttribute("agings",oAgings);
        List<OForecastType> oForecastTypes = forecastTypeService.selectOForecastTypeList(new OForecastType());
        mmap.addAttribute("forecastTypes",oForecastTypes);
        List<OProductType> oProductTypes = productTypeService.selectOProductTypeList(new OProductType());
        mmap.addAttribute("productTypes",oProductTypes);
        return prefix + "/add";
    }

    /**
     * 新增保存产品类别
     */
    @RequiresPermissions("product:product:add")
    @Log(title = "产品类别", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(OProduct oProduct)
    {
        int px = 0;
        List<OProduct> oProducts = oProductService.selectOProductList(new OProduct());
        for (int i = 0; i < oProducts.size(); i++) {
            if(oProducts.get(i).getSortOrder()>=px){
                px = oProducts.get(i).getSortOrder();
            }
        }
        oProduct.setResourcesId(171L);
        oProduct.setSortOrder(px);//设置排序
        oProduct.setIsstop(0);//默认启用
        oProduct.setPnametype(0);//产品命名时间类型(0世界时，1北京时，北京时减8小时)
        return toAjax(oProductService.insertOProduct(oProduct));
    }

    /**
     * 修改产品类别
     */
    @RequiresPermissions("product:product:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        List<SysDept> deptList = sysDeptService.selectDeptList(new SysDept());
        mmap.addAttribute("deptList",deptList);
        List<OProductRepairTime> oProductRepairTimes = productRepairTimeService.selectOProductRepairTimeList(new OProductRepairTime());
        mmap.addAttribute("repairTimes",oProductRepairTimes);
        List<OProductLateTime> oProductLateTimes = productLateTimeService.selectOProductLateTimeList(new OProductLateTime());
        mmap.addAttribute("productLateTimes",oProductLateTimes);
        List<OHistoryDays> oHistoryDays = historyDaysService.selectOHistoryDaysList(new OHistoryDays());
        mmap.addAttribute("historyDays",oHistoryDays);
        List<OForecastInterval> oForecastIntervals = forecastIntervalService.selectOForecastIntervalList(new OForecastInterval());
        mmap.addAttribute("forecastIntervals",oForecastIntervals);
        List<OFrequency> oFrequencies = frequencyService.selectOFrequencyList(new OFrequency());
        mmap.addAttribute("frequencies",oFrequencies);
        List<OForecastElement> oForecastElements = forecastElementService.selectOForecastElementList(new OForecastElement());
        mmap.addAttribute("elements",oForecastElements);
        List<OAging> oAgings = agingService.selectOAgingList(new OAging());
        mmap.addAttribute("agings",oAgings);
        List<OForecastType> oForecastTypes = forecastTypeService.selectOForecastTypeList(new OForecastType());
        mmap.addAttribute("forecastTypes",oForecastTypes);
        List<OProductType> oProductTypes = productTypeService.selectOProductTypeList(new OProductType());
        mmap.addAttribute("productTypes",oProductTypes);
        OProduct oProduct = oProductService.selectOProductById(id);
        mmap.put("oProduct", oProduct);
        return prefix + "/edit";
    }

    /**
     * 修改保存产品类别
     */
    @RequiresPermissions("product:product:edit")
    @Log(title = "产品类别", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(OProduct oProduct)
    {
        return toAjax(oProductService.updateOProduct(oProduct));
    }

    /**
     * 删除产品类别
     */
    @RequiresPermissions("product:product:remove")
    @Log(title = "产品类别", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(oProductService.deleteOProductByIds(ids));
    }

    /**
     * 修改产品类别
     */
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") String id, ModelMap mmap)
    {
        List<SysDept> deptList = sysDeptService.selectDeptList(new SysDept());
        mmap.addAttribute("deptList",deptList);
        List<OProductRepairTime> oProductRepairTimes = productRepairTimeService.selectOProductRepairTimeList(new OProductRepairTime());
        mmap.addAttribute("repairTimes",oProductRepairTimes);
        List<OProductLateTime> oProductLateTimes = productLateTimeService.selectOProductLateTimeList(new OProductLateTime());
        mmap.addAttribute("productLateTimes",oProductLateTimes);
        List<OHistoryDays> oHistoryDays = historyDaysService.selectOHistoryDaysList(new OHistoryDays());
        mmap.addAttribute("historyDays",oHistoryDays);
        List<OForecastInterval> oForecastIntervals = forecastIntervalService.selectOForecastIntervalList(new OForecastInterval());
        mmap.addAttribute("forecastIntervals",oForecastIntervals);
        List<OFrequency> oFrequencies = frequencyService.selectOFrequencyList(new OFrequency());
        mmap.addAttribute("frequencies",oFrequencies);
        List<OForecastElement> oForecastElements = forecastElementService.selectOForecastElementList(new OForecastElement());
        mmap.addAttribute("elements",oForecastElements);
        List<OAging> oAgings = agingService.selectOAgingList(new OAging());
        mmap.addAttribute("agings",oAgings);
        List<OForecastType> oForecastTypes = forecastTypeService.selectOForecastTypeList(new OForecastType());
        mmap.addAttribute("forecastTypes",oForecastTypes);
        List<OProductType> oProductTypes = productTypeService.selectOProductTypeList(new OProductType());
        mmap.addAttribute("productTypes",oProductTypes);
        OProduct oProduct = oProductService.selectOProductById(id);
        mmap.put("oProduct", oProduct);
        return prefix + "/detail";
    }

    /**
     * 使用单位——跳转
     * @param id
     * @param mmap
     * @return
     */
    @GetMapping("updateProductDept")
    public String updateProductDept(@RequestParam String id,ModelMap mmap){
        List<SysDept> deptList = sysDeptService.selectDeptList(new SysDept());
        mmap.addAttribute("deptList",deptList);
        OProduct oProduct = oProductService.selectOProductById(id);
        mmap.put("oProduct", oProduct);
        return prefix + "/productDept";
    }

    /**
     * 使用单位——更新数据
     * @param deptIds
     * @param productId
     * @return
     */
    @PostMapping("updateProductDept")
    @ResponseBody
    public AjaxResult updateProductDept(String deptIds, String productId){
        return toAjax(productDeptService.updateOProductDept(productId,deptIds));
    }

    @GetMapping("openFiles")
    public String openFiles(@RequestParam String id, ModelMap mmap){
        mmap.put("id",id);
        mmap.put("modalName","文档下载,新闻,法规");
        return prefix + "/files";
    }
}
